package com.tarena.lbs.coupon.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.common.utils.SnowflakeIdGenerator;
import com.tarena.lbs.coupon.api.pojo.param.UserCouponsParam;
import com.tarena.lbs.coupon.base.enums.CouponStatusEnum;
import com.tarena.lbs.coupon.dao.mapper.CouponMapper;
import com.tarena.lbs.coupon.dao.mapper.UserCouponsMapper;
import com.tarena.lbs.coupon.dao.repository.UserCouponsRepository;
import com.tarena.lbs.coupon.pojo.po.CouponPO;
import com.tarena.lbs.coupon.pojo.po.UserCouponsPO;
import com.tarena.lbs.coupon.pojo.vo.UserCouponsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZHC
 */
@Repository
@Slf4j
public class UserCouponsRepositoryImpl implements UserCouponsRepository {
    @Autowired
    private UserCouponsMapper userCouponsMapper;
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private SnowflakeIdGenerator idGenerator;

    @Override
    public List<UserCouponsVO> selectByUserIdAndCouponId(Integer userId, Integer couponId) {
        log.debug("UserCoupons数据层:selectByUserIdAndCouponId:{},{}",userId,couponId);
        QueryWrapper<UserCouponsPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id",couponId);
        queryWrapper.eq("user_id",userId);
        List<UserCouponsPO> userCouponsPOS = userCouponsMapper.selectList(queryWrapper);
        List<UserCouponsVO> userCouponsVOList = userCouponsPOS.stream()
                .map(po -> {
                    UserCouponsVO userCouponsVO = new UserCouponsVO();
                    BeanUtils.copyProperties(po,userCouponsVO);
                    return userCouponsVO;
                })
                .collect(Collectors.toList());
        return userCouponsVOList;
    }

    @Override
    public void insertCouponCode(UserCouponsParam param) {
        log.debug("UserCoupons数据层:insertCouponCode:{}",param);
        UserCouponsPO userCouponsPO = new UserCouponsPO();
        // 拷贝参数 shop_id、coupon_id、acticvity_id、coupon_code,不一定有值
        BeanUtils.copyProperties(param,userCouponsPO);
        userCouponsPO.setStatus(CouponStatusEnum.USER_COUPON_STATUS_UNUSED.getCode());
        Date date = new Date();
        userCouponsPO.setUpdateAt(date);
        userCouponsPO.setCreateAt(date);
        // 雪花算法生成优惠券code
        String couponCode = idGenerator.nextId()+"";
        userCouponsPO.setCouponCode(couponCode);
        CouponPO couponPO = couponMapper.selectById(param.getCouponId());
//        userCouponsPO.setOrderNo();   // 领取优惠券的时候，未使用
//        userCouponsPO.setUsedTime();  // 领取优惠券的时候，未使用
        userCouponsPO.setCouponType(couponPO.getCouponType()) ;
        userCouponsPO.setCouponValue(couponPO.getDiscountValue());
        userCouponsMapper.insert(userCouponsPO);
    }
}
